package com.ddtech.framework.system.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ddtech.framework.system.model.SysSpecialty;
import com.ddtech.framework.system.model.vo.SysSpecialtyTreeVo;
import com.ddtech.framework.system.service.SysSpecialtyService;
import com.ddtech.framework.system.service.dto.SysSpecialtyQuery;
import com.ddtech.framework.system.service.mapper.SysSpecialtyMapper;
import com.ddtech.utils.TreeUtils;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class SysSpecialtyServiceImpl extends ServiceImpl<SysSpecialtyMapper, SysSpecialty> implements SysSpecialtyService {
    @Override
    public void create(SysSpecialty sysSpecialty) {
        this.saveOrUpdate(sysSpecialty);
    }

    @Override
    public void del(String id) {
        SysSpecialty sysSpecialty = this.getById(id);
        List<String> ids = new ArrayList<>();
        ids.add(sysSpecialty.getId());
        Set<SysSpecialty> courseChapterSet = new HashSet<>();
        findSubType(ids, courseChapterSet);
        courseChapterSet.forEach(item -> {
            this.removeById(item.getId());
        });
    }

    @Override
    public Object getTree(SysSpecialtyQuery specialtyQuery) {
        List<SysSpecialty> courseTypeList = this.list(specialtyQuery.autoWrapper());
        List<SysSpecialtyTreeVo> data = courseTypeList.stream().map(map -> JSON.parseObject(JSON.toJSONString(map), SysSpecialtyTreeVo.class))
                .sorted(Comparator.comparing(SysSpecialtyTreeVo::getSort, Comparator.nullsFirst(Comparator.naturalOrder())))
                .collect(Collectors.toList());
        return TreeUtils.buildListToTree(data);
    }

    @Override
    public Set<SysSpecialty> getSubSpecialty(String id) {
        SysSpecialty sysSpecialty = this.getById(id);
        List<String> ids = new ArrayList<>();
        ids.add(sysSpecialty.getId());
        Set<SysSpecialty> courseChapterSet = new HashSet<>();
        findSubType(ids, courseChapterSet);
        return courseChapterSet;
    }

    @Override
    public SysSpecialty getSysSpecialtyByName(String name) {
        LambdaQueryWrapper<SysSpecialty> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysSpecialty::getLabel, name);
        List<SysSpecialty> list = this.list(queryWrapper);
        if (CollUtil.isNotEmpty(list)) {
            return list.get(0);
        }
        return null;
    }

    public void findSubType(List<String> ids, Set<SysSpecialty> sysSpecialtySet) {
        for (String id : ids) {
            SysSpecialtyQuery chapterQuery = new SysSpecialtyQuery();
            chapterQuery.setPid(id);
            List<SysSpecialty> list = this.list(chapterQuery.autoWrapper());
            if (CollUtil.isNotEmpty(list)) {
                List<String> collect = list.stream().map(SysSpecialty::getId).collect(Collectors.toList());
                findSubType(collect, sysSpecialtySet);
            }
            sysSpecialtySet.add(this.getById(id));
        }
    }
}
